﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;

namespace BoAddonBiz_FreightForwarder.Cost.Bo
{
    /// <summary>
    /// 业务订单行List
    /// </summary>
    public class OrderLines : List<OrderLine>
    {
        private OrderLine Changeline;
        private int _DocEntry = 10000;
        private int _LineNum = 1;
        /// <summary>
        /// 获取所有订单
        /// </summary>
        public void LoadBoOrders(SAPbouiCOM.DataTable DT_Rece, OrderType ordertyp)
        {
            OrderLine BorderLine;
            for (int Row = 0; Row < DT_Rece.Rows.Count; Row++)
            {
                BorderLine = this.Add(ordertyp);
                BorderLine.Status = Status_Line.Nochange;
                this.FillData_OrderLine(BorderLine, DT_Rece, Row);
            }
        }

        public OrderLine GetOrderLine(int DocEntry, int LineNum, OrderType ordertype)
        {
            foreach (OrderLine line in this)
            {
                if (line.DocEntry == DocEntry && line.LineNum == LineNum && line.OrderType == ordertype)
                {
                    return line;
                }
            }
            return null;
        }

        public IList<OrderLine> GetOrderLines(int DocEntry, int LineNum, OrderType ordertype)
        {
            IList<OrderLine> OrderLines = new List<OrderLine>();
            foreach (OrderLine Line in this)
            {
                if (Line.DocEntry == DocEntry && Line.LineNum == LineNum && Line.OrderType == ordertype)
                    OrderLines.Add(Line);
            }
            return OrderLines;
        }

        public OrderLine GetNewOrderLine(int DocEntry, int LineNum, OrderType ordertype)
        {
            OrderLine NewOrderLine = default(OrderLine);
            foreach (OrderLine Line in this)
            {
                if (Line.DocEntry == DocEntry && Line.LineNum == LineNum && Line.OrderType == ordertype && Line.Status == Status_Line.Add)
                {
                    NewOrderLine = Line;
                    break;
                }
            }
            return NewOrderLine;
        }

        /// <summary>
        /// 获取实体中m_字段
        /// </summary>
        private static List<FieldInfo> GetTypeDBFields(Type type)
        {
            List<System.Reflection.FieldInfo> rtFields = new List<FieldInfo>();
            FieldInfo[] FieldInfos = type.GetFields(BindingFlags.NonPublic | BindingFlags.Instance);

            string fieldNameStart = string.Empty;
            //字段名的前缀
            string[] prefix_fieldName = new string[] { "m_" };
            foreach (System.Reflection.FieldInfo item in FieldInfos)
            {
                fieldNameStart = item.Name.Substring(0, 2);
                if (item.IsPrivate && prefix_fieldName.Contains(fieldNameStart))
                {
                    rtFields.Add(item);
                }
            }
            return rtFields;
        }
        /// <summary>
        ///订单行实体填充值
        /// </summary>
        private void FillData_OrderLine(object obj_OrderLine, SAPbouiCOM.DataTable oDt, int RowIndex)
        {
            List<FieldInfo> List_FieldInfo_Line = GetTypeDBFields(typeof(OrderLine));
            string fieldName = string.Empty;
            foreach (FieldInfo FieldInfo_Order in List_FieldInfo_Line)
            {
                fieldName = FieldInfo_Order.Name.Substring(2);
                FieldInfo_Order.SetValue(obj_OrderLine, oDt.GetValue(fieldName, RowIndex));
            }
        }

        void BorderLine_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)
        {
            Changeline = (sender as OrderLine);

            if (e.PropertyName == "CardCode" && Changeline.Status != Status_Line.Add)
            {
                Changeline.Status = Status_Line.Add;
                Changeline.VisOrder = 1;
            }
        }

        void BorderLine_PropertyChanging(object sender, System.ComponentModel.PropertyChangingEventArgs e)
        {
            Changeline = (sender as OrderLine);
            if (Changeline.Status != Status_Line.Add && e.PropertyName == "CardCode")
            {
                bool Exists = false;
                foreach (OrderLine line in GetOrderLines(Changeline.DocEntry, Changeline.LineNum, Changeline.OrderType))
                {
                    if (line.Status == Status_Line.del)
                    {
                        Exists = true;
                        break;
                    }
                }
                if (!Exists)
                {
                    OrderLine NewLine = Changeline.Clone();
                    NewLine.Status = Status_Line.del;
                    this.Add(NewLine);
                }
            }
        }

        public OrderLine Add(OrderType ordertype)
        {
            OrderLine line = new OrderLine();
            line.Status = Status_Line.Add;
            line.DocEntry = _DocEntry;
            line.LineNum = _LineNum;
            line.OrderType = ordertype;
            line.VisOrder = 1;
            this.Add(line);
            line.PropertyChanging += new System.ComponentModel.PropertyChangingEventHandler(BorderLine_PropertyChanging);
            line.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(BorderLine_PropertyChanged);
            _DocEntry++;
            _LineNum++;
            return line;
        }
        public bool Remove(int DocEntry, int LineNum, OrderType ordertype)
        {
            IList<OrderLine> Lines = GetOrderLines(DocEntry, LineNum, ordertype);
            foreach (OrderLine Line in Lines)
            {
                if (Line.Status == Status_Line.Add)
                {
                    this.Remove(Line);
                }
                else
                    Line.Status = Status_Line.del;
            }
            return true;
        }
        //标记删除的记录
        public void Delete(OrderLine line)
        {
            this.GetOrderLine(line.DocEntry, line.LineNum, line.OrderType).VisOrder = -1111;
        }
        /// <summary>
        /// 修复订单中的行号
        /// </summary>
        public void UpdateVisOrder()
        {
            foreach (OrderLine item in this.Where<OrderLine>(line => line.VisOrder == -1111))
            {
                int i = 1;
                IList<OrderLine> lines = this.Where<OrderLine>(line => line.DocEntry == item.DocEntry && line.OrderType == item.OrderType && line.LineNum != item.LineNum).ToList();
                foreach (OrderLine n in lines)
                {
                    n.VisOrder = i;
                    i++;
                }

            }
            //int i = 1, j = 1;
            //foreach (OrderLine line in this)
            //{
            //    if (line.VisOrder == -1111) continue;
            //    if (line.OrderType == OrderType.SalesOrder)
            //    {
            //        line.VisOrder = i;
            //        i++;
            //    }
            //    if (line.OrderType == OrderType.Purchase)
            //    {
            //        line.VisOrder = j;
            //        j++;
            //    }
            //}
        }
        public new void Clear()
        {
            _DocEntry = 10000;
            _LineNum = 1;
            base.Clear();
        }
        public override string ToString()
        {
            return base.ToString() + " " + this.Count.ToString();
        }

        /// <summary>
        /// 判断结算单位是否为空
        /// </summary>
        /// <returns></returns>
        public bool IsCompanyNull()
        {
            foreach (OrderLine oitem in this)
            {
                if (string.IsNullOrEmpty(oitem.CardCode))
                    return true;
            }
            return false;
        }
    }
}
